# 24. 文本统计分析
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
let input = '';
rl.on('line', (line) => {
input += line + '\n';
});
rl.on('close', () => {
console.log(countTexts(input));
})
function countTexts(input) {
let count = 0;
let inString = false;
let inComment = false;
let stringDelimiter = '';
let isEmpty = true;
for(let i=0; i<input.length; i++) {
let c = input[i];
let nextChart = (i + 1 < input.length) ? input[i+1] : '\0';
if (inComment) {
if (c === '\n') {
inComment = false;
}
continue;
}
if (c==='-' && nextChar === '-' && !inString) {
inComment = true;
i++;
continue;
}
if (c==='\'' || c === '\"' && !inString) {
inString = true;
stringDelimiter = c;
isEmpty = false;
continue;
}
if (c === stringDelimiter && inString) {
if (nextChar === stringDelimiter) {
i++;
} else {
inString = false;
}
continue;
}
if (c===';' && !inString) {
if (!isEmpty) {
count++;
isEmpty = true;
}
continue;
}
if (!/\s/.test(c) && !inString) {
isEmpty = false;
}
}
if (!isEmpty) {
count++;
}
return count;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67